#include "string.h"

namespace _WX
{
	const size_t string::npos = -1;
	string::string(const char* str)
		:_size(strlen(str))
	{
		_str = new char[_size + 1 ];
		strcpy(_str, str);
		_capacity = _size;
	}

	string::string(const string& s1)
		//:_size(strlen(s1._str))
	{
		_str = new char[_capacity + 1];
		strcpy(_str, s1._str);
		_capacity = s1._capacity;
		_size = s1._size;
	}
	
	string::~string()
	{
		if (_str != nullptr)
		{
			delete[] _str;
			_str = nullptr;
		}
		_size = _capacity;
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		if (_size + 1 > _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size] = ch;
		_size++;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			size_t newcapacity = _capacity * 2;
			if (newcapacity < len + _size)
			{
				newcapacity = len + _size;
			}
			reserve(newcapacity);
		}
		strcpy(_str + _size, str);
		_size += len;
	}

	void string::insert(size_t pos, size_t n, char ch)
	{
		if (n + _size > _capacity)
		{
			size_t newcapacity = _capacity * 2;
			if (newcapacity < n + _size)
			{
				newcapacity = n + _size;
			}
			reserve(newcapacity);
		}
		size_t end = _size + n;
		while (end > pos + n - 1)
		{
			_str[end] = _str[end - n];
			end--;
		}

		for (int i = 0; i < n; i++)
		{
			_str[pos + i] = ch;
		}
		_size += n;

	}

	void string::insert(size_t pos, const char* str)
	{
		size_t n = strlen(str);
		insert(pos, n, 'x');
		for (int i = 0; i < n; i++)
		{
			_str[pos + i] = str[i];
		}
	}

	void string::erase(size_t pos , size_t n )
	{
		if (n > _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			size_t end = pos + n;
			while (_size >= end)
			{
				_str[end - n] = _str[end];
				end++;
			}
			_size -= n;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		for (int i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
				return i;
		}
		return npos;
	}

	size_t string::find(const char* str, size_t pos)
	{
		char* tmp = strstr(_str + pos, _str);
		if (tmp == nullptr)
		{
			return npos;		
		}
		else
		{
			return tmp - _str;
		}
		
	}

	string& string::operator=(const string& s)
	{
		char* tmp = new char[s._capacity + 1];
		strcpy(tmp, s._str);
		delete[] _str;
		_str = tmp;
		_capacity = s._capacity;
		_size = s._size;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
	}

	string& string::operator+=(const char* str)
	{
		append(str);
	}


	ostream& operator<<(ostream& out, const string& s)
	{
		for (auto e : s)
		{
			cout << e;
		}
		return out;
	}

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}

	istream& operator>>(istream& in, string& s)
	{
		const size_t N = 100;
		int i = 0;
		char buff[N];
		char ch = in.get();
		while (ch != ' ' && ch != '\0')
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
			ch = in.get();
		}
		if (i != 0)
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}
	
	istream& getline(istream& in, string& s, char delim)
	{
		s.clear();

		const size_t N = 1024;
		char buff[N];
		int i = 0;
		char ch = in.get();
		while (ch != delim)
		{
			buff[i++] = ch;
			if (i == N - 1)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}

			ch = in.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}
}

void test1()
{
	_WX::string s1("12345");
	//s1.push_back('x');
	//s1.push_back('x');
	//s1.push_back('x');
	//s1.append("yyyy");
	s1.insert(2,2,'x');
	s1.insert(0, "abc");
	cout << s1.c_str();

}